<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>promise实现定时变色</title>
    <style>
        .rect {
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
    </style>
</head>
<body>
<div class="rect"></div>
<script>
    var rect = document.getElementsByClassName('rect')[0];

    function asyncChangeColor(color, wait) {
        return new Promise(resolve => {
            setTimeout(() => {
                rect.style.backgroundColor = color;
                resolve()
            }, wait)
        })
    }

    async function changeColorLoop() {
        await asyncChangeColor('red',1000);
        await asyncChangeColor('orange',1000);
        await asyncChangeColor('black',1000);
        await asyncChangeColor('pink',1000);
        await changeColorLoop();
    }
    changeColorLoop();
</script>
</body>
</html>
